package cc.rengu.igas.mcps.common.dao.impl;

import cc.rengu.igas.mcps.common.dao.TxnMapper;
import cc.rengu.igas.mcps.common.entity.TransAmtInfo;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.FinanceTxn;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 金融交易流水表操作接口类.
 */
public class TxnMapperImpl implements TxnMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);


    /**
     * 根据条件查询交易总金额与交易总笔数
     * 根据商户号、终端号、批次号、交易码、币种、交易状态(成功)且冲正状态（未冲正）条件
     *
     * @param mchntNo  商户号
     * @param termNo   终端号
     * @param txnNum   交易码
     * @param batchNo  批次号
     * @param transCcy 币种
     * @return 交易总金额与交易总笔数
     * @throws Exception 异常
     */
    @Override
    public TransAmtInfo selectTransAmtAndTransNumByCondition(String mchntNo, String termNo, String txnNum, String batchNo, String transCcy) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTransAmtAndTransNumByCondition");
        FinanceTxn financeTxn = new FinanceTxn();
        financeTxn.setMchntNo(mchntNo);
        financeTxn.setTermNo(termNo);
        financeTxn.setTxnNum(txnNum);
        financeTxn.setOrderCcy(transCcy);
        financeTxn.setTransBatchNo(batchNo);
        TransAmtInfo transAmtInfo = new TransAmtInfo();
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, financeTxn, transAmtInfo);
        if (0 != iReturnCode) {
            rglog.error("查询交易流水表失败,mchntNo:<{}>,termNo:<{}>,batchNo:<{}>,txnNum:<{}>,transCcy:<{}>", mchntNo, termNo, batchNo, txnNum, transCcy);
            return null;
        }
        return transAmtInfo;
    }
}
